package Operations;

import util.Filter;
import GA.MethodDeclared;

public class Invoke extends Operation{
	private final String _return;
	private final String _this;
	private final MethodDeclared method;
	private  String[] params;
	public Invoke(String _return, String _this, MethodDeclared method, String[] params) {
		this._return = _return;
		this._this = _this;
		this.method = method;
		this.params = params;
	}
	public String get_return() {
		return _return;
	}
	public String getThis() {
		return _this;
	}

	public MethodDeclared getMethod() {
		return method;
	}

	public String[] getParams() {
		return params;
	}
	public void setParams(String[] params) {
		this.params=params;
	}
	
	@Override
	public Operation clone() {
		MethodDeclared m=method.clone();
		m.setReplacementObjectParam(method.getReplacementObjectParam());
		final Invoke clone = new Invoke(_return, _this, m, params.clone());
		return clone;
	}

	@Override
	public String toString() {

		String ret = "";
		if(this.method.getReturnType()!=null)
		if(!Filter.isPrimitiveOrStringType(this.method.getReturnType()))
				 ret =this._return+ " = ";
	
	//	if(_return != null)
//			ret = _return + " = ";
		Class<?>[] paramsTpe=this.method.getParameterTypes();
		
		StringBuilder pars = null;
		for(int i=0;i<params.length;i++) {
			//Object value= ValuesGenerator.mapValueToObject(paramsTpe[i],params[i]);
			if(pars == null) pars = new StringBuilder();
			else pars.append(", ");
			if(params[i]==null)
				pars.append("null");
			else
				if(paramsTpe[i].equals(java.lang.String.class)||(Filter.isObjectType(paramsTpe[i])&&(method.getReplacementObjectParam().equals(java.lang.String.class))) )
					pars.append("\"" + params[i].toString() + "\"" );
				else
					if(paramsTpe[i].equals(char.class))
						pars.append("\'" + params[i].toString() + "\'" );
					else
					pars.append( params[i].toString()  );
		}


		return ret + (method.isStatic() ? this.method.getClazz().getName()+ "." : _this.toString()+ ".") + method.getName() + "(" + (pars != null ? pars.toString() : "") + ")";
	//	return ret + (method.isStatic() ? " " : " "+ ".") + method.getName() + "(" + (pars != null ? pars.toString() : "") + ")";
	}

}

